/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 *   
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#include "TouchMessenger.h"

int TouchMessenger::Do(std::vector<Blob>* trackedBlobList, std::vector<Blob>* removedBlobList)
{
	currentTime = TUIO::TuioTime::getSessionTime();
	tuioServer->initFrame(currentTime);

	// TUIO protocal : process Event

	// add and update
	TUIO::TuioCursor* cursor = NULL;
	for(unsigned int i=0; i<trackedBlobList->size(); i++)
	{
		int id = (*trackedBlobList)[i].id;
		float x = (float)(*trackedBlobList)[i].pt.x;
		float y = (float)(*trackedBlobList)[i].pt.y;

		switch((*trackedBlobList)[i].eventMessage)
		{
		case Blob::MEMSSAGE_ADD:
			cursor = tuioServer->addTuioCursor(x, y);
			stickyCursorList[id] = cursor;
			break;
		case Blob::MEMSSAGE_MOVE:
			cursor = stickyCursorList[id];
			tuioServer->updateTuioCursor(cursor, x, y);
			break;
		case Blob::MEMSSAGE_HOLD:
			// TODO : to serve Hold message
			cursor = stickyCursorList[id];
			tuioServer->updateTuioCursor(cursor, x, y);
			break;
		}
	}

	// remove
	for(unsigned int i=0; i<removedBlobList->size(); i++)
	{
		int id = (*removedBlobList)[i].id;
		cursor = stickyCursorList[id];
		tuioServer->removeTuioCursor(cursor);
		std::map<int, TUIO::TuioCursor*>::iterator iter = stickyCursorList.find(id);
		stickyCursorList.erase(iter);
	}
	
	tuioServer->stopUntouchedMovingCursors();
	tuioServer->commitFrame();

	return 0;
}